package web.suzy.oj.manager.msg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.SecurityUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import web.suzy.oj.dao.msg.AdminSysNoticeEntityService;
import web.suzy.oj.dao.msg.UserSysNoticeEntityService;
import web.suzy.oj.pojo.entity.msg.AdminSysNotice;
import web.suzy.oj.pojo.entity.msg.UserSysNotice;
import web.suzy.oj.pojo.vo.SysMsgVO;
import web.suzy.oj.shiro.AccountProfile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * YangSuzy 软件工作室
 * 类名: NoticeManager
 * 描述: ---- 待定 -----
 * 功能: 负责处理用户通知
 *
 * @author YangSuzy
 * Date: 2022/11/18 17:19
 */
@Component
public class NoticeManager {

    @Resource
    private UserSysNoticeEntityService userSysNoticeEntityService;

    @Resource
    private AdminSysNoticeEntityService adminSysNoticeEntityService;

    @Resource
    private ApplicationContext applicationContext;

    /**
     * 方法名: getSysNotice
     * 描述: 获取当前用户接收的系统消息
     *
     * @param limit       每页显示数量
     * @param currentPage 当前页
     * @return com.baomidou.mybatisplus.core.metadata.IPage<web.suzy.oj.pojo.vo.SysMsgVO>
     * @date 2022/11/18 17:20
     * @auther YangSuzy
     **/
    public IPage<SysMsgVO> getSysNotice(Integer limit, Integer currentPage) {
        //设置默认页码
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        //设置默认每页显示数目
        if (limit == null || limit < 1) {
            limit = 5;
        }
        //获取当前登录的用户
        AccountProfile userRolesVo = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        IPage<SysMsgVO> sysNotice = userSysNoticeEntityService.getSysNotice(limit, currentPage, userRolesVo.getUid());
        applicationContext.getBean(NoticeManager.class).updateSysOrMineMsgRead(sysNotice);
        return sysNotice;
    }

    /**
     * 方法名: getMineNotice
     * 描述: 获取当前用户接收的我的消息
     *
     * @param limit       每页显示数目
     * @param currentPage 当前页
     * @return com.baomidou.mybatisplus.core.metadata.IPage<web.suzy.oj.pojo.vo.SysMsgVO>
     * @date 2022/11/18 17:25
     * @auther YangSuzy
     **/
    public IPage<SysMsgVO> getMineNotice(Integer limit, Integer currentPage) {
        //设置默认页码
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        //设置默认每页显示数目
        if (limit == null || limit < 1) {
            limit = 5;
        }
        //获取当前登录的用户
        AccountProfile userRolesVo = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        IPage<SysMsgVO> mineNotice = userSysNoticeEntityService.getMineNotice(limit, currentPage, userRolesVo.getUid());
        applicationContext.getBean(NoticeManager.class).updateSysOrMineMsgRead(mineNotice);
        return mineNotice;
    }

    /**
     * 方法名: updateSysOrMineMsgRead
     * 描述: 异步更新消息已读状态
     *
     * @param userMsgList 用户消息列表
     * @return void
     * @date 2022/11/18 17:38
     * @auther YangSuzy
     **/
    @Async
    public void updateSysOrMineMsgRead(IPage<SysMsgVO> userMsgList) {
        List<Long> idList = userMsgList.getRecords().stream()
                .filter(userMsgVo -> !userMsgVo.getState())
                .map(SysMsgVO::getId)
                .collect(Collectors.toList());
        if (idList.size() == 0) {
            return;
        }
        UpdateWrapper<UserSysNotice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", idList)
                .set("state", true);
        userSysNoticeEntityService.update(null, updateWrapper);
    }

    /**
     * 方法名: syncNoticeToNewRegisterUser
     * 描述: 异步方法，将公告消息转发给新注册的用户
     *
     * @param uid 新注册的用户uuid
     * @return void
     * @date 2022/11/18 17:51
     * @auther YangSuzy
     **/
    @Async
    public void syncNoticeToNewRegisterUser(String uid) {
        QueryWrapper<AdminSysNotice> adminSysNoticeQueryWrapper = new QueryWrapper<>();
        //查询所有在当前时间之前已发送过给用户的消息
        adminSysNoticeQueryWrapper
                .eq("type", "All")
                .le("create_time", new Date())
                .eq("state", true);
        List<AdminSysNotice> adminSysNotices = adminSysNoticeEntityService.list(adminSysNoticeQueryWrapper);
        if (adminSysNotices.size() == 0) {
            return;
        }
        List<UserSysNotice> userSysNoticeList = new ArrayList<>();
        //转为用户系统消息批量发送给新注册的用户
        for (AdminSysNotice adminSysNotice : adminSysNotices) {
            UserSysNotice userSysNotice = new UserSysNotice();
            userSysNotice.setType("Sys")
                    .setSysNoticeId(adminSysNotice.getId())
                    .setRecipientId(uid);
            userSysNoticeList.add(userSysNotice);
        }
        userSysNoticeEntityService.saveOrUpdateBatch(userSysNoticeList);
    }
}
